/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Definiciones.java
 *
 * Created on 24-oct-2011, 21:52:56
 */
package gui.ventanas;

import archivos.AGuardar;
import archivos.Guardador;
import automatas.*;
import compiladores.*;
import enumeraciones.Alfabetos;
import excepciones.*;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Arturo
 */
public class Definiciones extends javax.swing.JPanel implements ItemListener {

    private static final long serialVersionUID = 1L;
    /** Creates new form viewDefinicionyGrafos */
    private Alfabeto alf = null;
    Map<String, NFA> mapadeNfas = null;
    DefRegular reg = null;
    Guardador guardador;
    AGuardar aGuardar;

    /** Creates new form Definiciones */
    
    public void nullateComponets(){
        this.alf= null;
        this.mapadeNfas = null;
        this.reg = null;
        this.guardador=null;
        this.aGuardar=null;
    }
    public Definiciones() {
        initComponents();
        guardador = new Guardador();
        this.jLabel3.setVisible(false);
        this.alfDefinido.setVisible(false);

        this.jLabel7.setVisible(false);
        this.jLabel8.setVisible(false);

        comboBoxAlfabetos.addItem("Seleccione Un Alfabeto");
        comboBoxAlfabetos.addItem("a y b");
        comboBoxAlfabetos.addItem("0 y 1");
        comboBoxAlfabetos.addItem("Letras Mayusculas");
        comboBoxAlfabetos.addItem("Letras Minusculas");
        comboBoxAlfabetos.addItem("Todas las Letras");
        comboBoxAlfabetos.addItem("Todas las letras y numeros");
        comboBoxAlfabetos.addItem("Numeros");
        comboBoxAlfabetos.addItem("Definir Alfabeto");
        comboBoxAlfabetos.addItemListener(this);
    }

    public void itemStateChanged(ItemEvent ie) {
        alf=null;
        String s = (String) ie.getItem();
        if (s.equalsIgnoreCase("Definir Alfabeto")) {
            this.jLabel3.setVisible(true);
            this.alfDefinido.setVisible(true);
            this.alfDefinido.setEditable(true);
            this.alfDefinido.setText("");
            this.jLabel7.setVisible(true);
            this.jLabel8.setVisible(true);
        } else if (!s.equalsIgnoreCase("Seleccione un Alfabeto")) {
            alf = new Alfabeto(Alfabetos.fromString(s));
            this.alfDefinido.setEditable(false);
            this.jLabel3.setVisible(false);
            this.alfDefinido.setVisible(false);

            this.jLabel7.setVisible(false);
            this.jLabel8.setVisible(false);
        } else {
            alf = null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        comboBoxAlfabetos = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        alfDefinido = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        nombreExp = new javax.swing.JTextField();
        exp = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        nombreDef = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N
        jTabbedPane1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTabbedPane1MouseClicked(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.addHierarchyListener(new java.awt.event.HierarchyListener() {
            public void hierarchyChanged(java.awt.event.HierarchyEvent evt) {
                jPanel1HierarchyChanged(evt);
            }
        });

        comboBoxAlfabetos.setName("comboBoxAlfabetos"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(Definiciones.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        alfDefinido.setEditable(false);
        alfDefinido.setName("alfDefinido"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jPanel5.setName("jPanel5"); // NOI18N

        jLabel4.setFont(jLabel4.getFont().deriveFont((jLabel4.getFont().getStyle() | java.awt.Font.ITALIC) | java.awt.Font.BOLD, jLabel4.getFont().getSize()+7));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        nombreExp.setName("nombreExp"); // NOI18N

        exp.setName("exp"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nombre", "Expresión Regular"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton6.setText(resourceMap.getString("jButton6.text")); // NOI18N
        jButton6.setName("jButton6"); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(246, 246, 246)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel5Layout.createSequentialGroup()
                                .addComponent(nombreExp, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(257, 257, 257))
                                    .addComponent(exp))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton2)
                            .addComponent(jButton1))))
                .addContainerGap(241, Short.MAX_VALUE))
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 789, Short.MAX_VALUE))
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton6)
                .addContainerGap(716, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(16, 16, 16)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nombreExp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel7.setForeground(resourceMap.getColor("jLabel7.foreground")); // NOI18N
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setForeground(resourceMap.getColor("jLabel8.foreground")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        nombreDef.setName("nombreDef"); // NOI18N
        nombreDef.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nombreDefActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setEnabled(false);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton4MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(34, 34, 34)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(nombreDef)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(comboBoxAlfabetos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(alfDefinido, javax.swing.GroupLayout.DEFAULT_SIZE, 271, Short.MAX_VALUE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 306, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(nombreDef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(comboBoxAlfabetos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(alfDefinido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(4, 4, 4)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel8)
                        .addGap(16, 16, 16)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton4)))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 880, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel1HierarchyChanged(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_jPanel1HierarchyChanged
        this.validate();
}//GEN-LAST:event_jPanel1HierarchyChanged

    public void aGregarATabla(String expNombre, String expr) {

        DefaultTableModel modelo = (DefaultTableModel) this.jTable1.getModel();
        Object[] fila = new Object[2];
        fila[0] = expNombre;
        fila[1] = expr;
        int filas = modelo.getRowCount();
        int columnas = modelo.getColumnCount();
        if (filas == 0) {
            modelo.addRow(fila);
        } else {
            boolean agregar = true;
            for (int i = 0; i < columnas; i++) {
                for (int j = 0; j < filas; j++) {
                    Object o = modelo.getValueAt(j, i);
                    if (((modelo.getValueAt(j, i))).equals(fila[0])) {
                        agregar = false;
                        JOptionPane.showMessageDialog(this, "Ya existe");
                        break;
                    }
                }
                if (agregar == false) {
                    break;
                }
            }
            if (agregar == true) {
                modelo.addRow(fila);
                this.nombreExp.setText("");
                this.exp.setText("");
            }
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String expNombre = this.nombreExp.getText();
        String expre = this.exp.getText();
        this.aGregarATabla(expNombre, expre);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        DefaultTableModel modelo = (DefaultTableModel) this.jTable1.getModel();
        int filas = modelo.getRowCount();
        if (filas == 0) {
            JOptionPane.showMessageDialog(this, "Tabla Vacía");
        } else if (this.jTable1.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Seleccione una Expresión a Eliminar");
        } else {
            int fila = this.jTable1.getSelectedRow();
            modelo.removeRow(fila);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void nombreDefActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
}                                         

private void jButton4MouseClicked(java.awt.event.MouseEvent evt) {                                      
        JFileChooser fileChooser = new JFileChooser();
        int seleccion = fileChooser.showSaveDialog(this);
        if (seleccion != JFileChooser.APPROVE_OPTION) {
            return;
        }




        guardador.Guardar(aGuardar, fileChooser.getSelectedFile());
}                                     

                                 

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        //this.nullateComponets();
        this.jButton4.setEnabled(true);
        try {
            if (nombreDef == null || nombreDef.getText().equalsIgnoreCase("")) {
                JOptionPane.showMessageDialog(this, "Defina un nombre para su Definición");
            } 
            if (!this.alfDefinido.getText().equalsIgnoreCase("")&& this.alfDefinido.isEnabled()) {  // auxParaNfas definido por usuario
                try {
                    this.alf = new Alfabeto(this.alfDefinido.getText());

                } catch (CaracterNoValidoEnExpresionRegularException ex) {
                    JOptionPane.showMessageDialog(this, "El alfabeto Definido contiene caracteres Especiales. "
                            + "\n Por favor redefina su alfabeto");

                }
            } else if (alf==null) {
                JOptionPane.showMessageDialog(this, "Seleccione o defina un Alfabeto");
            }
            
            if (alf != null && nombreDef != null && !nombreDef.getText().equalsIgnoreCase("")) {
                // ya tenemos el auxParaNfas.. podemos crear la definicion regular..
                reg = new DefRegular(alf);
                reg.setName(nombreDef.getText());

                List<String> expresiones = new ArrayList<String>();
                DefaultTableModel modelo = (DefaultTableModel) this.jTable1.getModel();
                int filas = modelo.getRowCount();
                int columnas = 2;
                for (int j = 0; j < filas; j++) {
                    String nombre = (String) modelo.getValueAt(j, 0);
                    String expresion = (String) modelo.getValueAt(j, 1);
                    expresiones.add(nombre + " -> " + expresion);
                }
                reg.poblar(expresiones);
                //

                aGuardar = new AGuardar();
                aGuardar.setDefRegular(reg);
        

                Expresiones nuevo = new Expresiones();
                nuevo.poblar(aGuardar);
                

                jTabbedPane1.addTab(nombreDef.getText(), nuevo);
                //mapadeNfas = reg.generarNFAs();
            }
        } catch (ExpresionIncorrectaException ex) {
             JOptionPane.showMessageDialog(this, "Expresion Regular definida Incorrectamente");
        } catch (LetraNoPerteneceAlfabeto ex) {
             JOptionPane.showMessageDialog(this, "Error: Letra no pertenece al alfabeto\n " + ex.getMessage());
        } catch (ParentesisDesvalanceadosException ex) {
            JOptionPane.showMessageDialog(this, "Error: Parentesis desvalanceados en Expresión Regular");
        } catch (IdentificadorNoValidoException ex) {
           JOptionPane.showMessageDialog(this, "Identificador no valido" );
        } catch (IdentificadorNoEncontradoException ex) {
            JOptionPane.showMessageDialog(this, "Identificador no encontrado. \n" + ex.getMessage());
        } catch (CaracterNoValidoEnExpresionRegularException ex) {
            JOptionPane.showMessageDialog(this, "Error: Letra no pertenece al alfabeto" + ex.getMessage());
        } catch (StackOverflowError ex){
            JOptionPane.showMessageDialog(this, "Error: Referencias Cruzadas entre Identificadores");
        }
}//GEN-LAST:event_jButton6ActionPerformed


    

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked

        JFileChooser fileChooser = new JFileChooser();
        int seleccion = fileChooser.showOpenDialog(this);
        if (seleccion != JFileChooser.APPROVE_OPTION) {
            return;
        }
        File fichero = fileChooser.getSelectedFile();
        AGuardar cargado = guardador.Cargar(fichero);
        
        if (cargado != null) {
            this.aGuardar = cargado;
            DefRegular def = aGuardar.getDefRegular();
//            this.nombreDef.setText(def.getName());
//            for (Entry<String, ExprRegular> entry : aGuardar.getDefRegular().regs.entrySet()) {
//                aGregarATabla(entry.getKey(), entry.getValue().cadena);
//            }
//            if (def.alfabeto.alfabetos!=null){
//                this.comboBoxAlfabetos.setSelectedItem(def.alfabeto.alfabetos.toString());
//            }
//            else{
//                this.comboBoxAlfabetos.setSelectedItem("Definir Alfabeto");
//                String letras = new String (def.alfabeto.letras).replace(constantes.letras.empty, "");
//                String toRep = letras;
//               for(int i = 0 ; i < (Alfabeto.SEspeciales + Alfabeto.SAgrupadores).toCharArray().length; i++){
//                   char letra = (Alfabeto.SEspeciales + Alfabeto.SAgrupadores).toCharArray()[i];
//                   if (letras.indexOf(letra)!=-1){
//                       toRep = letras.replace(""+letra, (new String("\\"))+letra);
//                   }
//               }
//              
//                this.alfDefinido.setText(toRep);
//                jLabel3.setVisible(true);
//                jLabel7.setVisible(true);
//                jLabel8.setVisible(true);
//                alfDefinido.setVisible(true);
//            }

            Expresiones nuevo = new Expresiones();
        
            nuevo.poblar(aGuardar);
         
           jTabbedPane1.addTab(def.getName(), nuevo);
           
               
            
            
        }
        else 
        {
            JOptionPane.showMessageDialog(this, "El archivo esta dañado y no puede ser cargado "
                                + "\nSeleccione otro por favor");
        }

        // y a trabajar con fichero ....

    }//GEN-LAST:event_jButton3MouseClicked

    private void jTabbedPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTabbedPane1MouseClicked
        // TODO add your handling code here:
        System.out.println(evt.getButton());
        if (evt.getButton() == 2 && jTabbedPane1.getSelectedIndex() != 0)
            jTabbedPane1.remove(jTabbedPane1.getSelectedIndex());
    }//GEN-LAST:event_jTabbedPane1MouseClicked

  




       

                                        

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JTextField alfDefinido;
    public javax.swing.JComboBox comboBoxAlfabetos;
    private javax.swing.JTextField exp;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    public javax.swing.JButton jButton3;
    public javax.swing.JButton jButton4;
    public javax.swing.JButton jButton6;
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    public javax.swing.JLabel jLabel7;
    public javax.swing.JLabel jLabel8;
    public javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    public javax.swing.JTextField nombreDef;
    private javax.swing.JTextField nombreExp;
    // End of variables declaration//GEN-END:variables
}
